<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <link rel="Shortcut Icon" href="../images/aecc.ico" type="image/x-icon" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <link rel="stylesheet" href="../StaticFiles/element-ui/element-ui.css">
  <link rel="stylesheet" href="../StaticFiles/nprogress/nprogress.min.css">
  <link rel="stylesheet" href="../styles/vue-page-common.css">
  <script src="../Scripts/vue.min.js"></script>
  <script src="../StaticFiles/element-ui/element-ui.js"></script>
  <script src="../Scripts/axios.min.js"></script>
  <script src="../StaticFiles/nprogress/nprogress.min.js"></script>
  <script src="../Scripts/dayjs.min.js"></script>
  <script src="../Scripts/axios.config.js"></script>
  <title>转工看板</title>
</head>

<body>
  <div id="app" v-cloak v-on:keyup.enter="getData">
    <el-row class="header">
      <el-col :span="6"><img src="../images/aeec_logo.png" height="60" alt="黎阳LOGO" /></el-col>
      <el-col :span="12" class="header-title">{{title}}</el-col>
      <el-col :span="6" class="currentTime">{{currentTime}}</el-col>
    </el-row>

    <div style="padding:10px;">
      <el-form :inline="true" :model="queryInfo" ref="queryInfoRef" :label-position="'left'">
        <el-form-item label="主制分厂:">
          <el-select v-model="queryInfo.factoryid" placeholder="请选择主制分厂" filterable style="width: 140px;">
            <el-option v-for="f in factoryList" :key="f.factoryid" :label="f.factoryname" :value="f.factoryid">
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="承制分厂:">
          <el-select v-model="queryInfo.tofactoryid" placeholder="请选择承制分厂" filterable style="width: 160px;" clearable>
            <el-option v-for="f in tofactoryList" :key="f.factoryid" :label="f.factoryname" :value="f.factoryid">
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item prop='containername' label="流水卡号:">
          <el-autocomplete v-model.trim="queryInfo.containername" placeholder="请输入流水卡号" :trigger-on-focus="false"
            style="width:240px;" :fetch-suggestions="searchContainerAsync" :value-key="'containername'" :debounce="500">
          </el-autocomplete>
        </el-form-item>

        <el-form-item prop="status" label="转工状态:">
          <el-select v-model="queryInfo.status" placeholder="请选择转工状态" clearable filterable>
            <el-option v-for="s in statusList" :key="s.status" :label="s.status" :value="s.status">
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="转出时间:">
          <el-date-picker v-model="queryInfo.senddate" type="daterange" range-separator="至" value-format="yyyy-MM-dd"
            start-placeholder="开始日期" end-placeholder="结束日期" :picker-options="pickerOptions"></el-date-picker>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="small" @click="getData()">查询</el-button>
          <el-button type="primary" icon="el-icon-download" size="small" @click="getExportExcel">导出</el-button>
        </el-form-item>
      </el-form>

      <!-- 表格显示区 -->
      <el-table v-loading="loading" v-loading.fullscreen.lock="loading" :element-loading-text="loadingText"
        element-loading-background="rgba(0,0,0,0.8)" :data="dataList" stripe border>
        <el-table-column type="index" width="40"></el-table-column>
        <el-table-column label="批次" prop="togetherflag"></el-table-column>
        <el-table-column label="转出工序" prop="specname" width="200" show-overflow-tooltip></el-table-column>
        <el-table-column label="数量" prop="qty" width="60"></el-table-column>
        <el-table-column label="转出人" prop="outperson" width="60"></el-table-column>
        <el-table-column label="承制分厂" prop="factoryname" width="120"></el-table-column>
        <el-table-column label="转出日期" prop="submitdate" width="100" :formatter="splitDate"></el-table-column>
        <el-table-column label="要求完成时间" prop="plandate" width="100" :formatter="splitDate"></el-table-column>
        <el-table-column label="预计完成时间" prop="estimatedcompletedate" width="100" :formatter="splitDate">
        </el-table-column>
        <el-table-column label="接收时间" prop="toacceptdate" width="100" :formatter="splitDate"></el-table-column>
        <el-table-column label="完成日期" prop="completedate" width="100" :formatter="splitDate"></el-table-column>
        <el-table-column label="转入日期" prop="returndate" width="100" :formatter="splitDate"></el-table-column>
        <el-table-column label="超期天数" prop="delaydays" width="100">
          <template slot-scope="scope">
            <div v-if="scope.row.delaydays > 0" style="color:red;font-weight: bold;">
              <i class="el-icon-warning"></i>
              {{scope.row.delaydays}} 天
            </div>
            <div v-else>
              {{scope.row.delaydays}} 天
            </div>
          </template>
        </el-table-column>
        <el-table-column label="状态" prop="status" width="80">
          <template slot-scope="scope" v-if="scope.row.status">
            <div v-if="scope.row.status === '已转出'">
              <el-tag type='warning'>已转出</el-tag>
            </div>
            <div v-else-if="scope.row.status === '已接收'">
              <el-tag>已接收</el-tag>
            </div>
            <div v-else-if="scope.row.status === '已完成'">
              <el-tag type='success'>已完成</el-tag>
            </div>
            <div v-else-if="scope.row.status === '已转回'">
              <el-tag type='info'>已转回</el-tag>
            </div>
            <div v-else-if="scope.row.status === '已拒收'">
              <el-tag type='danger'>已拒收</el-tag>
            </div>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页组件 -->
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
        :current-page="queryInfo.currentPage" :page-sizes="[16,50,100]" :page-size="queryInfo.pageSize"
        layout="prev,pager,next,jumper,sizes,total" :total="total">
      </el-pagination>

      <!-- 回到顶部 -->
      <el-backtop></el-backtop>
    </div>
  </div>

  <script type="module">
    import { getParams, fetchSuggestion, exportExcelData } from '../Scripts/hooks/useCommonMethod.js';
    import { getFactoryListByCode, getUserInfo } from '../Scripts/hooks/fetchAPI.js';
    import { prevWeek, thisWeek, nextWeek, prevMonth, thisMonth, nextMonth } from '../Scripts/hooks/useDateShortCut.js'
    import { splitDate } from '../Scripts/hooks/format.js'
    const app = new Vue({
      el: '#app',
      data: {
        splitDate,
        currentTime: '',// 当前时间
        title: '',// 看板标题
        queryInfo: {
          factoryid: '',
          tofactoryid: '',
          factoryname: '',
          containername: '',
          senddate: ''
        },
        total: 0,// 总条数 
        currentPage: 1,//当前页
        pageSize: 16, //每页显示条数
        loading: false,
        loadingText: '拼命加载中',
        dataList: [],
        statusList: [
          { status: '已转出' },
          { status: '已接收' },
          { status: '已完成' },
          { status: '已转回' },
          { status: '已拒收' }
        ],
        factoryList: [],
        tofactoryList: [],
        userInfo: {},
        pickerOptions: { shortcuts: [prevWeek, thisWeek, nextWeek, prevMonth, thisMonth, nextMonth] }
      },
      methods: {
        // 查询批次号
        searchContainerAsync(value, callback) {
          fetchSuggestion({ actionName: 'getContainernameAsync', name: 'containername', value, callback })
        },
        //查询数据
        async getData(currentpage) {
          this.loading = true;
          this.loadingText = '数据查询中...';
          const { data: { data, meta, total } } = await axios({
            method: 'post',
            url: '../handler/board/ContainerShipRecordLookBoard.ashx?action=getData',
            data: {
              action: 'getData',
              currentPage: this.currentPage,
              pageSize: this.pageSize,
              ...this.queryInfo
            }
          })
          this.loading = false;
          if (meta.status === 500) { return this.$message.error(meta.msg); }
          this.dataList = data;
          this.total = total;
        },
        // 导出excel
        async getExportExcel() {
          const config = {
            url: '../handler/board/ContainerShipRecordLookBoard.ashx?action=getExportExcel',
            data: { action: 'getExportExcel', ...this.queryInfo }
          }
          exportExcelData(config, this);
        },
        //监听 currentpage 改变
        handleCurrentChange(newPage) {
          this.currentPage = newPage;
          this.getData();
        },
        //监听 pagezie 改变
        handleSizeChange(newSize) {
          this.pageSize = newSize;
          this.getData();
        }
      },
      async mounted() {
        let incrementNumber = () => {
          setTimeout(incrementNumber, 500);
          this.currentTime = dayjs().format('YYYY年MM月DD日  HH:mm:ss');
        }
        setTimeout(incrementNumber, 500);
        const [userInfo, factoryList, tofactoryList] = await Promise.all([
          getUserInfo({ employeeid: getParams("uid") }),
          getFactoryListByCode({ factoryCode: '601|607|609|600|608' }),
          getFactoryListByCode({ factoryCode: '305|602|601|607|609|600|608' })
        ]);

        this.userInfo = userInfo;
        this.factoryList = factoryList;
        this.tofactoryList = tofactoryList;

        if (this.userInfo.factoryid && this.factoryList.some(item => item.factoryid === this.userInfo.factoryid)) {
          const { factoryid, factoryname } = this.userInfo;
          this.queryInfo.factoryid = factoryid;
          this.queryInfo.factoryname = factoryname;
        } else {
          this.queryInfo.factoryid = this.factoryList[0].factoryid;
          this.queryInfo.factoryname = this.factoryList[0].factoryname;
        }
        this.title = `${this.queryInfo.factoryname}转工看板`;
        document.title = this.title;
        this.getData();
      },
      watch: {
        queryInfo: {
          handler() {
            this.currentPage = 1;
            this.queryInfo.factoryname = this.factoryList.filter(item => item.factoryid === this.queryInfo.factoryid)[0].factoryname;
            this.title = `${this.queryInfo.factoryname}转工看板`;
            document.title = this.title;
          },
          deep: true
        }
      }
    })
  </script>
</body>

</html>